# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# Author: Emanuel Fonseca
# Email:  emdfonseca<at>gmail<dot>com
# Date:   25 August 2008
#
# Author: Eugene Kin Chee Yip
# Date:   7 Nov 2008
#
# Author: Richard Penman
# Date:   12 Nov 2009


import cjson

from ofc_elements import title, x_legend, y_legend, x_axis, y_axis, radar_axis, tooltip


class Template(dict):
    def __init__(self, title_string, style=None):
        self['title'] = title(title_string, style)
        self.set_bg_colour('FFFFFF')
        self.set_x_axis(grid_colour='FFFFFF')
        self.set_y_axis(grid_colour='FFFFFF')

    def set_x_legend(self, legend, style=None):
        self['x_legend'] = x_legend(legend, style)

    def set_y_legend(self, legend, style=None):
        self['y_legend'] = y_legend(legend, style)

    def set_x_axis(self, stroke=None, tick_height=None, colour=None, grid_colour=None, labels=None, three_d=None, max=None, min=None, steps=None, offset=None):
        self['x_axis'] = x_axis(stroke, tick_height, colour, grid_colour, labels, three_d, max, min, steps, offset)

    def set_y_axis(self, stroke=None, tick_length=None, colour=None, grid_colour=None, labels=None, max=None, min=None, steps=None, offset=None):
        self['y_axis'] = y_axis(stroke, tick_length, colour, grid_colour, labels, max, min, steps, offset)

    def set_y_axis_right(self, stroke=None, tick_length=None, colour=None, grid_colour=None, labels=None, max=None, min=None, steps=None, offset=None):
        self['y_axis_right'] = y_axis(stroke, tick_length, colour, grid_colour, labels, max, min, steps, offset)

    def set_radar_axis(self, stroke=None, tick_height=None, colour=None, grid_colour=None, labels=None, max=None, min=None, steps=None, spoke_labels=None):
        self['radar_axis'] = radar_axis(stroke, tick_height, colour, grid_colour, labels, max, min, steps, spoke_labels)

    def set_bg_colour(self, colour):
        self['bg_colour'] = colour

    def set_tooltip(self, shadow=None, stroke=None, colour=None, bg_colour=None, title_style=None, body_style=None, behaviour=None):
        self['tip'] = tooltip(shadow, stroke, colour, bg_colour, title_style, body_style, behaviour)

    def add_element(self, element):
        try:
            self['elements'].append(element)
        except:
            self['elements'] = [element]

    def encode(self):
        return cjson.encode(self)
